var apis = require('../../cloud/lib/apis');
var rongrequest = require('../../cloud/lib/rongrequest');
var AV = require('leanengine');

exports.publish = publish;
function publish(fromUserId, toUserId, objectName, content, format, callback) {
    rongrequest.request(apis['message']['publish'], {
        fromUserId: fromUserId,
        toUserId: toUserId,
        objectName: objectName,
        content: content
    }, format, function (err, resultText) {
        return callback(err, resultText);
    });
}
exports.privatePublic = privatePublic;
function privatePublic(fromUserId, toUserId, objectName, content, format, callback) {
    rongrequest.request(apis['message']['privatePublic'], {
        fromUserId: fromUserId,
        toUserId: toUserId,
        objectName: objectName,
        content: content
    }, format, function (err, resultText) {
        return callback(err, resultText);
    });
}
exports.broadcast = function (fromUserId, objectName, content, format, callback) {
    rongrequest.request(apis['message']['broadcast'], {
        fromUserId: fromUserId,
        objectName: objectName,
        content: content
    }, format, function (err, resultText) {
        return callback(err, resultText);
    });
}


exports.pushGroupMsg = pushGroupMsg;
function pushGroupMsg(fromUserId, toGroupId, objectName, content, format, callback) {

    console.log(content);

    rongrequest.request(apis['message']['pushGroupMsg'], {
        fromUserId: fromUserId,
        toGroupId: toGroupId,
        objectName: objectName,
        content: content
    }, format, function (err, resultText) {
        return callback(err, resultText);
    });
}

exports.pushChatroomMsg = pushChatroomMsg;
function pushChatroomMsg(fromUserId, toChatroomId, objectName, content, format, callback) {
    rongrequest.request(apis['message']['pushChatroomMsg'], {
        fromUserId: fromUserId,
        toChatroomId: toChatroomId,
        objectName: objectName,
        content: JSON.stringify(content)
    }, format, function (err, resultText) {
        return callback(err, resultText);
    });
}


//发送点赞数
exports.sendLikeNum = function (fromUserId, toChatroomId, nums, callback) {

    var content = {};
    content.name = "likeNum";
    content.data = {};
    content.data.num = nums;

    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}


//发送踢出指令
exports.sendKickUser = function (fromUserId, toChatroomId, userId, callback) {

    var content = {};
    content.name = "kickUser";
    content.data = {};
    content.data.userId = userId;


    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}

//发送禁言指令
exports.sendGagUser = function (fromUserId, toChatroomId, userId, callback) {

    var content = {};
    content.name = "gagUser";
    content.data = {};
    content.data.userId = userId;

    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}

//发送取消禁言指令
exports.sendUnGagUser = function (fromUserId, toChatroomId, userId, callback) {

    var content = {};
    content.name = "unGagUser";
    content.data = {};
    content.data.userId = userId;

    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}


//发送直播结束指令
exports.sendLiveEnd = function (fromUserId, toChatroomId, callback) {
    var content = {};
    content.name = "liveEnd";
    content.data = {};

    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}
//发送直播状态指令
exports.sendLiveStatus = function (fromUserId, toChatroomId, status, callback) {

    var content = {};
    content.name = "liveStatus";
    content.data = {};
    content.data.status = status;

    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}


//发送加入直播指令
exports.sendJoinLive = function (fromUserId, toChatroomId, userId, callback) {

    var content = {};
    content.name = "joinLive";
    content.data = {};
    content.data.userId = userId;


    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}


//发送加入直播指令
exports.sendJoinLive = function (fromUserId, toChatroomId, userId, callback) {

    var content = {};
    content.name = "joinLive";
    content.data = {};
    content.data.userId = userId;


    pushChatroomMsg(fromUserId, toChatroomId, "RC:CmdNtf", content, "json", callback);
}


//发送消息上传成功
exports.sendCircleUploadSuccess = function (fromUserId, toUser, circleId, callback) {
    var content = {};
    content.name = "CircleUploadSuccess";
    content.data = {};
    content.data.circleId = circleId;
    publish(fromUserId, toUser, "RC:CmdNtf", JSON.stringify(content), "json", callback);
}
exports.sendHasNewCircleMsg = function (fromUserId, userAvatar, toUser, data, callback) {
    var content = {};
    content.name = "HasNewCircleMsg";
    content.data = data;
    content.data.userAvatar = userAvatar;
    content.data.comment.userAvatar = userAvatar;
    content.data.laud.userAvatar = userAvatar;
    publish(fromUserId, toUser, "RC:CmdNtf", JSON.stringify(content), "json", callback);

}
exports.sendCustomApply = function (fromUserId, toUser, callback) {
    var content = {};
    content.name = "customApply";
    publish(fromUserId, toUser, "RC:CmdNtf", JSON.stringify(content), "json", callback);
}

exports.sendCircleCoverUploadSuccess = function (fromUserId, toUser, callback) {
    var content = {};
    content.name = "CircleCoverUploadSuccess";
    content.data = {};
    publish(fromUserId, toUser, "RC:CmdNtf", JSON.stringify(content), "json", callback);
}
exports.addTribe = function (userId, groupId, userName, callback) {
    var content = {};
    content.operation = "Add";
    content.operatorUserId = userId;
    content.message = "加入部落";
    content.extra = userName + "加入部落";

    var data = {};
    var targetUserIds = [];
    targetUserIds.push(userId);
    //var targetUserDisplayNames = [];
    //targetUserDisplayNames.push("ceshi");
    //data.targetUserIds = targetUserIds;
    data.operatorNickname = userName;
    //data.targetUserDisplayNames = targetUserDisplayNames;
    content.data = data;
    //pushGroupMsg(userId,groupId,"RC:GrpNtf",JSON.stringify(content),"json",callback);
}


exports.yaoqingTribe = function (userId, groupId, userName, yaoqingid, yaoqingname, callback) {
    var content = {};
    content.operation = "Add";
    content.operatorUserId = userId;
    content.message = "加入部落";
    content.extra = userName + "加入部落";

    var data = {};
    data.targetUserIds = yaoqingid;
    data.operatorNickname = userName;
    data.targetUserDisplayNames = yaoqingname;

    content.data = data;

    //pushGroupMsg(userId,groupId,"RC:GrpNtf",JSON.stringify(content),"json",callback);
}

exports.delTribeUser = function (userId, groupId, userName, targetUserIds, targetUserDisplayNames, callback) {
    var content = {};
    content.operation = "Kicked";
    content.operatorUserId = userId;
    content.message = "";
    content.extra = userName + "";

    var data = {};
    data.targetUserIds = targetUserIds;
    data.operatorNickname = userName;
    data.targetUserDisplayNames = targetUserDisplayNames;

    content.data = data;

    //pushGroupMsg(userId,groupId,"RC:GrpNtf",JSON.stringify(content),"json",callback);
}

exports.quitTribe = function (userId, groupId, userName, callback) {
    var content = {};
    content.operation = "Quit";
    content.operatorUserId = userId;
    content.message = "";
    content.extra = userName + "";

    var data = {};
    var targetUserIds = [];
    targetUserIds.push(userId);
    var targetUserDisplayNames = [];
    targetUserDisplayNames.push(userName);
    data.targetUserIds = targetUserIds;
    data.operatorNickname = userName;
    data.targetUserDisplayNames = targetUserDisplayNames;

    content.data = data;

    //pushGroupMsg(userId,groupId,"RC:GrpNtf",JSON.stringify(content),"json",callback);
}
exports.sendSystemMessage = sendSystemMessage;
function sendSystemMessage(toUserOId,content,type) {
    // 推送交易消息
    var msg_content = {};
    msg_content["content"] = content;

    var msg_extra = {};
    msg_extra["extra"] = type; //交易消息代码
    msg_extra["code"] = apis['message_type_code']['word']; //文本消息
    msg_extra["content"] = msg_content;

    var base64Content = new Buffer(JSON.stringify(msg_extra)).toString('base64');
    base64Content = base64Content.replace(/\+/g, "@");
    base64Content = base64Content.replace(/=/g, "");

    var sendObj = {};
    sendObj["operation"] = "";
    sendObj["sourceUserId"] = "";
    sendObj["targetUserId"] = "";
    sendObj["extra"] = base64Content;
    publish("admin", toUserOId, "RC:ContactNtf", JSON.stringify(sendObj), "json", function (err, result1) {
    });
}

